// pages/address/detail/detail.js
import {AddressModel} from '../../../model/address'
import { config } from '../../../config'
var address = new AddressModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    switch:false,
    bottom:0,
    detail:{
      isDefault:0
    },
  },

  onNameChange(e){
    this.data.detail.userName = e.detail.value
  },

  onMobileChange(e){
    this.data.detail.userMobile = e.detail.value
  },

  onAddressChange(e){
    this.data.detail.province = e.detail.value
  },

  onMinuteChange(e){
    this.data.detail.minuteAddress = e.detail.value
  },

  onAllAddressChange(e){
    this.data.detail.allAddress = e.detail.value
  },

  chooseAction(){
    let _this = this;
    wx.authorize({
      scope: 'scope.userLocation',
      success () {
        console.log('aaaa')
        wx.chooseLocation({
          success:res=>{
            _this.data.detail.province = res.address
            _this.data.detail.city = ""
            _this.data.detail.area = ""
            _this.setData({
              detail:_this.data.detail
            })
          }
        })
      },
      fail(){
        console.log('bbbb')
      }
    })
  },

  defaultChange:function(e){
    this.data.detail.isDefault = e.detail.value?1:0
  },

  addressAction(){
    address.save(this.data.detail).then(
      res=>{
        if (res.code===0) {
          wx.navigateBack()
        }
      }
    )
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      bottom:config.statusBarHeight
    })
    if (options.id) {
      address.detail(options.id).then(
        res=>{
          delete res.data.updateTime
          delete res.data.createTime
          this.setData({
            detail:res.data,
          })
        }
      )
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})